import request from '@/utils/request'

// ==================== 对话管理 API ====================

/**
 * 获取用户会话列表
 * @param {string} userId - 用户ID
 * @param {number} limit - 返回数量限制
 */
export function getConversations(userId, limit = 20) {
  return request({
    url: '/api/ai/chat/conversations',
    method: 'get',
    params: { userId, limit }
  })
}

/**
 * 获取会话详情
 * @param {string} conversationId - 会话ID
 * @param {string} userId - 用户ID
 */
export function getConversation(conversationId, userId) {
  return request({
    url: `/api/ai/chat/dify/conversations/${conversationId}`,
    method: 'get',
    params: { userId }
  })
}

/**
 * 删除会话
 * @param {string} conversationId - 会话ID
 * @param {string} userId - 用户ID
 */
export function deleteConversation(conversationId, userId) {
  return request({
    url: `/api/ai/chat/dify/conversations/${conversationId}`,
    method: 'delete',
    params: { userId }
  })
}

/**
 * 重命名会话
 * @param {string} conversationId - 会话ID
 * @param {string} userId - 用户ID
 * @param {string} name - 新名称
 * @param {boolean} autoGenerate - 是否自动生成名称
 */
export function renameConversation(conversationId, userId, name, autoGenerate = false) {
  return request({
    url: `/api/ai/chat/dify/conversations/${conversationId}/name`,
    method: 'post',
    params: { userId, name, autoGenerate }
  })
}

/**
 * 获取归档会话列表
 * @param {string} userId - 用户ID
 * @param {number} limit - 返回数量限制
 */
export function getArchivedConversations(userId, limit = 20) {
  return request({
    url: '/api/ai/chat/dify/conversations/archived',
    method: 'get',
    params: { userId, limit }
  })
}

/**
 * 归档会话
 * @param {string} conversationId - 会话ID
 * @param {string} userId - 用户ID
 */
export function archiveConversation(conversationId, userId) {
  return request({
    url: `/api/ai/chat/conversations/${conversationId}/archive`,
    method: 'post',
    params: { userId }
  })
}

/**
 * 恢复归档会话
 * @param {string} conversationId - 会话ID
 * @param {string} userId - 用户ID
 */
export function unarchiveConversation(conversationId, userId) {
  return request({
    url: `/api/ai/chat/dify/conversations/${conversationId}/unarchive`,
    method: 'post',
    params: { userId }
  })
}

// ==================== 消息管理 API ====================

/**
 * 获取会话消息列表
 * @param {string} conversationId - 会话ID
 * @param {string} userId - 用户ID
 * @param {number} limit - 返回数量限制
 */
export function getMessages(conversationId, userId, limit = 20) {
  return request({
    url: `/api/ai/chat/conversations/${conversationId}/messages`,
    method: 'get',
    params: { conversationId, userId, limit }
  })
}

/**
 * 获取消息详情
 * @param {string} messageId - 消息ID
 * @param {string} userId - 用户ID
 */
export function getMessage(messageId, userId) {
  return request({
    url: `/api/ai/chat/dify/messages/${messageId}`,
    method: 'get',
    params: { userId }
  })
}

/**
 * 删除消息
 * @param {string} messageId - 消息ID
 * @param {string} userId - 用户ID
 */
export function deleteMessage(messageId, userId) {
  return request({
    url: `/api/ai/chat/dify/messages/${messageId}`,
    method: 'delete',
    params: { userId }
  })
}

/**
 * 添加消息反馈
 * @param {string} messageId - 消息ID
 * @param {string} userId - 用户ID
 * @param {string} rating - 评分 (like/dislike)
 * @param {string} comment - 评论
 */
export function addMessageFeedback(messageId, userId, rating, comment) {
  return request({
    url: `/api/ai/chat/dify/messages/${messageId}/feedbacks`,
    method: 'post',
    params: { userId, rating, comment }
  })
}

/**
 * 获取消息反馈
 * @param {string} messageId - 消息ID
 * @param {string} userId - 用户ID
 */
export function getMessageFeedback(messageId, userId) {
  return request({
    url: `/api/ai/chat/dify/messages/${messageId}/feedbacks`,
    method: 'get',
    params: { userId }
  })
}

/**
 * 停止消息生成
 * @param {string} taskId - 任务ID
 * @param {string} userId - 用户ID
 */
export function stopMessageGeneration(taskId, userId) {
  return request({
    url: `/api/ai/chat/dify/chat-messages/${taskId}/stop`,
    method: 'post',
    params: { userId }
  })
}

/**
 * 获取消息建议
 * @param {string} messageId - 消息ID
 * @param {string} userId - 用户ID
 */
export function getMessageSuggestions(messageId, userId) {
  return request({
    url: `/api/ai/chat/dify/messages/${messageId}/suggestions`,
    method: 'get',
    params: { userId }
  })
}
